﻿Public Class ListItem
    Public FileName As String
    Public Version As Double
    Public Binary As Byte()
    Public Properties As Hashtable

    Private _primaryId As Integer

    Public Sub New(ByVal properties As Hashtable, ByVal binary As Byte(), ByVal version As Double, ByVal fileName As String)
        Me.Properties = properties
        Me.Binary = binary
        Me.Version = version
        Me.FileName = fileName
    End Sub

    Public Sub AddTo(ByVal web As SPWeb, ByVal listId As Guid, ByRef id As Integer)

        Dim modifiedby As SPUser = web.AllUsers.GetByID(CInt(Me.Properties("Modified By").ToString.Split(CChar(";"))(0)))
        Dim createdby As SPUser = web.AllUsers.GetByID(CInt(Me.Properties("Created By").ToString.Split(CChar(";"))(0)))

        Dim userToken As SPUserToken = modifiedby.UserToken
        Using mySite As New SPSite(web.Url, userToken)
            Using myWeb As SPWeb = mySite.OpenWeb
                Dim list As SPList = myWeb.Lists.GetList(listId, False)
                'check out item if required and if item exists

                If id > -1 AndAlso list.ForceCheckout Then
                    list.GetItemById(id).File.CheckOut()
                End If
                ' Dim existingFile As SPFile = list.GetItemById(Me.Properties("Id").ToString)


                Dim file As SPFile = list.RootFolder.Files.Add(Me.FileName, Me.Binary, Me.Properties, True)
                Me._primaryId = file.Item.ID

                If CDbl(CInt(Me.Version)) = Me.Version Then
                    If list.ForceCheckout Then
                        file.CheckIn(Me.Properties("Check In Comment").ToString)
                    End If

                    file.Publish(Me.Properties("Check In Comment").ToString)

                    If list.EnableModeration Then
                        If Me.Properties.ContainsKey("Approver Comments") Then
                            file.Approve(Me.Properties("Approver Comments").ToString)
                        Else
                            file.Approve("")
                        End If
                    End If

                    file.Item("Created") = Me.Properties("Created")
                    file.Item("Modified") = Me.Properties("Modified") 'SPUtility.CreateISO8601DateTimeFromSystemDateTime(mySPItem.Modified)
                    file.Item("Author") = createdby.ID & ";#" & createdby.Name
                    file.Item("Editor") = modifiedby.ID & ";#" & modifiedby.Name
                    If Me.Properties.ContainsKey("Approver Comments") Then
                        file.Item("Approver Comments") = Me.Properties("Approver Comments").ToString
                    End If
                    file.Item.SystemUpdate()

                Else
                    file.Item("Created") = Me.Properties("Created")
                    file.Item("Modified") = Me.Properties("Modified")
                    file.Item("Author") = createdby.ID & ";#" & createdby.Name
                    file.Item("Editor") = modifiedby.ID & ";#" & modifiedby.Name
                    file.Item.UpdateOverwriteVersion()
                    If list.ForceCheckout Then
                        file.CheckIn(Me.Properties("Check In Comment").ToString)
                    End If
                End If
                id = file.Item.ID
                list.Update()
            End Using
        End Using
    End Sub

    Public Function GetPrimaryId() As Integer
        Return _primaryId
    End Function
End Class

